常用 Git 命令清单

创建时间:2018/5/11 9:30
标签:微信
来源:http://mp.weixin.qq.com/s?__biz=MzI3NzE0NjcwMg==&mid=2650121280&idx=1&sn=9328ce2cdf371538ca480b2e2bc66621&chksm=f36bb961c41c30779f41b8601fb9d091aabe6d05fdcd961de9d26574d4d6b8bb1739577c01e8&mpshare=1&scene=1&srcid=051120n6J7mXXHcQA8xZuwnr#rd

常用 Git 命令清单

Hollis 2018-05-11

作者 阮一峰

我们每天使用 Git ,但是很多命令记不住。

一般来说,日常使用只要记住下图6个命令,就可以了。但是熟练使用,恐怕要记住60~100个命令。

下面是我整理的常用 Git 命令清单,清单是供大家查缺补漏的,大家可以预览下以下清单,看自己有哪些是不太熟悉或者不知道的,然后针对性的了解其用法。熟练了这些命令可以大大提高效率。

几个专用名词的译名如下。

   
  • Workspace:工作区


  • Index / Stage:暂存区


  • Repository:仓库区(或本地仓库)


  • Remote:远程仓库


一、新建代码库
$ git init $ git init [project-name]

$ git clone [url]
二、配置

Git的设置文件为.gitconfig,它可以在用户主目录下(全局配置),也可以在项目目录下(项目配置)。

$ git config --list $ git config -e [--global]

$ git config [--global] user.name "[name]"

$ git config [--global] user.email "[email address]"
三、增加/删除文件
$ git add [file1][file2]...

$ git add [dir]

$ git add .

$ git add -p $ git rm [file1][file2]...

$ git rm --cached [file]

$ git mv [file-original][file-renamed]
四、代码提交
$ git commit -m [message]

$ git commit [file1][file2]...-m [message]

$ git commit -a $ git commit -v $ git commit --amend -m [message]

$ git commit --amend [file1][file2]...
五、分支
$ git branch $ git branch -r $ git branch -a $ git branch [branch-name]

$ git checkout -b [branch]

$ git branch [branch][commit]

$ git branch --track [branch][remote-branch]

$ git checkout [branch-name]

$ git checkout -

$ git branch --set-upstream [branch][remote-branch]

$ git merge [branch]

$ git cherry-pick [commit]

$ git branch -d [branch-name]

$ git push origin --delete [branch-name]

$ git branch -dr [remote/branch]
六、标签
$ git tag $ git tag [tag]

$ git tag [tag][commit]

$ git tag -d [tag]

$ git push origin :refs/tags/[tagName]

$ git show [tag]

$ git push [remote][tag]

$ git push [remote]--tags $ git checkout -b [branch][tag]
七、查看信息
$ git status $ git log $ git log --stat $ git log -S [keyword]

$ git log [tag] HEAD --pretty=format:%s $ git log [tag] HEAD --grep feature $ git log --follow [file]

$ git whatchanged [file]

$ git log -p [file]

$ git log -5--pretty --oneline $ git shortlog -sn $ git blame [file]

$ git diff $ git diff --cached [file]

$ git diff HEAD $ git diff [first-branch]...[second-branch]

$ git diff --shortstat "@{0 day ago}"

$ git show [commit]

$ git show --name-only [commit]

$ git show [commit]:[filename]

$ git reflog
八、远程同步
$ git fetch [remote]

$ git remote -v $ git remote show [remote]

$ git remote add [shortname][url]

$ git pull [remote][branch]

$ git push [remote][branch]

$ git push [remote]--force $ git push [remote]--all
九、撤销
$ git checkout [file]

$ git checkout [commit][file]

$ git checkout .

$ git reset [file]

$ git reset --hard $ git reset [commit]

$ git reset --hard [commit]

$ git reset --keep [commit]

$ git revert [commit]

$ git stash
$ git stash pop
十、其他
$ git archive

转载声明:本文系转载,原文地址:http://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html

- MORE | 更多精彩文章 -

如果你看到了这里,说明你喜欢本文。

那么请长按二维码,关注Hollis

转发朋友圈,是对我最大的支持。

    阅读原文